package com.justplay.mobilelocation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.justplay.mobilelocation.UI.ContactUsActivity;
import com.justplay.mobilelocation.bean.LoginResultBean;
import com.justplay.mobilelocation.util.SharePreferencesUtil;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.justplay.mobilelocation.MainActivity.myMainActivity;

public class LoginActivity extends Activity {

    @Bind(R.id.et_useName)
    EditText et_useName;
    @Bind(R.id.et_pwd)
    EditText et_pwd;
    @Bind(R.id.btn_login)
    Button btn_login;
    @Bind(R.id.tv_register)
    TextView tv_register;
    @Bind(R.id.tv_contact)
    TextView tv_contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.btn_login, R.id.tv_register, R.id.tv_contact})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                goLogin();
                break;
            case R.id.tv_register:
                startActivity(new Intent(this, RegisterActivity.class));
                finish();
                break;
            case R.id.tv_contact:
                startActivity(new Intent(this, ContactUsActivity.class));

                break;
        }
    }

    private void goLogin() {
        new HttpUtil.Builder("users/login")
                .Params("username", et_useName.getText().toString())
                .Params("password", et_pwd.getText().toString())
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        LoginResultBean loginResultBean = new Gson().fromJson(model, LoginResultBean.class);
                        if (loginResultBean.getFlag() == 1) {
//                            Toast.makeText(LoginActivity.this, model, Toast.LENGTH_SHORT).show();
                            SharePreferencesUtil.putString("userName", et_useName.getText().toString(), LoginActivity.this);
                            if (loginResultBean.getResult().getAuthorizationState().equals("1")) {
                                SharePreferencesUtil.putBoolean("havedBuy",true,LoginActivity.this);
                            }else{
                                SharePreferencesUtil.putBoolean("havedBuy", false, LoginActivity.this);

                            }
                            finish();
                        }else{
                            Toast.makeText(LoginActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                        }


                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String)values[1];
                Toast.makeText(LoginActivity.this, "操作失败", Toast.LENGTH_SHORT).show();

            }
        })
                .post();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        myMainActivity.finish();
    }
}
